﻿using System;
using System.Collections.Generic;
using System.Linq;
using HtmlAgilityPack;

namespace PhysEffects.HtmlParcer
{
    static class  HtmlNodeExtension
    {

        public static bool IsHeaderNode(this HtmlNode node)
        {
            string nodeTag = node.Name;

            if (nodeTag.Length == 2)
            {
                if (nodeTag[0].Equals('h') && Char.IsDigit(nodeTag[1]))
                {
                    return true;
                }
            }
            return false;
        }

        public static bool HasParent(this HtmlNode node, string parentName)
        {
            if (node.ParentNode == null)
            {
                return false;
            }
            else
            {
                if (node.ParentNode.Name.Equals(parentName))
                {
                    return true;
                }
                else
                {
                    return HasParent(node.ParentNode, parentName);
                }
            }
        }

        public static bool HasParentHeaderNode(this HtmlNode node)
        {
            if (node.ParentNode == null)
            {
                return false;
            }
            else
            {
                if (node.IsHeaderNode())
                {
                    return true;
                }
                else
                {
                    return HasParentHeaderNode(node.ParentNode);
                }
            }
        }
    }
}
